/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws. 
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability 
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the 
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2018, 2022 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name        : Config_OSTM0_user.c
* Component Version: 1.4.0
* Device(s)        : R7F702300EABA
* Description      : This file implements device driver for Config_OSTM0.
***********************************************************************************************************************/
/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_userdefine.h"
#include "Config_OSTM0.h"
/* Start user code for include. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
static volatile unsigned int sys_tick = 0;
/*
bit0:1ms
bit1:10ms
bit2:100ms
bit3:1s
bit4:1m
bit5:1h
bit6:1day
*/
static volatile unsigned int sys_tick_1ms = 0;
static volatile unsigned int sys_tick_10ms = 0;
static volatile unsigned int sys_tick_100ms = 0;
static volatile unsigned int sys_tick_1s = 0;
static volatile unsigned int sys_tick_1m = 0;
static volatile unsigned int sys_tick_1h = 0;
static volatile unsigned int sys_tick_1day = 0;

volatile unsigned int tick_flag = 0;
/* End user code. Do not edit comment generated here */

/***********************************************************************************************************************
* Function Name: R_Config_OSTM0_Create_UserInit
* Description  : This function adds user code after initializing OSTM module.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void R_Config_OSTM0_Create_UserInit(void)
{
    /* Start user code for user init. Do not edit comment generated here */
    sys_tick = 0;
    tick_flag = 0;
    sys_tick_1ms = 0;
    sys_tick_10ms = 0;
    sys_tick_100ms = 0;
    sys_tick_1s = 0;
    sys_tick_1m = 0;
    sys_tick_1h = 0;
    sys_tick_1day = 0;
    /* End user code. Do not edit comment generated here */
}

/***********************************************************************************************************************
* Function Name: r_Config_OSTM0_interrupt
* Description  : This function handles the INTOSTM0 interrupt.
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
#pragma interrupt r_Config_OSTM0_interrupt(enable=false, channel=199, fpu=true, callt=false)
void r_Config_OSTM0_interrupt(void)
{
    /* Start user code for r_Config_OSTM0_interrupt. Do not edit comment generated here */
    sys_tick++;
    if ((tick_flag & 0x01) == 0x01)
    {
        //
    }
    
    if (0 == sys_tick % 10)
    {
        if ((tick_flag & 0x02) == 0x02)
        {
            //10ms flag has not been cleaned! It is unreasonable!!!
        }
        else
        {
            tick_flag |= 0x02;
        }
        sys_tick_10ms++;
        if (0 == sys_tick_10ms % 10)
        {
            if ((tick_flag & 0x04) == 0x04)
            {
                //100ms flag has not been cleaned! It is unreasonable!!!
            }
            else
            {
                tick_flag |= 0x04;
            }
            sys_tick_100ms++;
            if (0 == sys_tick_100ms % 10)
            {
                if ((tick_flag & 0x08) == 0x08)
                {
                    //1s flag has not been cleaned! It is unreasonable!!!
                }
                else
                {
                    tick_flag |= 0x08;
                }
                sys_tick_1s++;
                if (0 == sys_tick_1s % 60)
                {
                    if ((tick_flag & 0x10) == 0x10)
                    {
                        //1m flag has not been cleaned! It is unreasonable!!!
                    }
                    else
                    {
                        tick_flag |= 0x10;
                    }
                    sys_tick_1m++;
                    if (0 == sys_tick_1m % 60)
                    {
                        if ((tick_flag & 0x20) == 0x20)
                        {
                            //1m flag has not been cleaned! It is unreasonable!!!
                        }
                        else
                        {
                            tick_flag |= 0x20;
                        }
                        sys_tick_1h++;
                        if (0 == sys_tick_1h % 24)
                        {
                            tick_flag |= 0x40;
                            sys_tick_1day++;
                        }
                    }
                }
            }
        }
    }
    /* End user code. Do not edit comment generated here */
}

/* Start user code for adding. Do not edit comment generated here */
unsigned int get_systick(void)
{
    /* return the global tick */
    return sys_tick;
}

unsigned int get_systick_flag(void)
{
    /* return the global tick */
    return tick_flag;
}

void set_systick(unsigned int tick)
{
    sys_tick = tick;
}
/* End user code. Do not edit comment generated here */
